package com.example.education.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.education.Bean.CollegeInfo;
import com.example.education.Mapper.CollegeInfoMapper;
import com.example.education.service.CollegeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚
 * @since 2023-03-27
 */
@Service
@Transactional
public class CollegeInfoServiceImpl extends ServiceImpl<CollegeInfoMapper, CollegeInfo> implements CollegeInfoService {


    @Autowired
    private CollegeInfoMapper collegeInfoMapper;


    @Override
    public int insert(CollegeInfo collegeInfo) {
        if (collegeInfo.getUid() == null) {
            return collegeInfoMapper.insert(collegeInfo);
        } else {
            return collegeInfoMapper.insert(collegeInfo);
        }
    }

    //根据学校名字查找学校
    public CollegeInfo selectByCollegeName(String collegeName) {
        return collegeInfoMapper.select(collegeName);
    }

    @Override
    public int delete(Integer uid) {
        return 0;
    }

    @Override
    public int update(CollegeInfo collegeInfo) {
        return 0;
    }

    @Override
    public CollegeInfo select(String collegename) {

        return collegeInfoMapper.select(collegename);

    }

    //查询所有大学
    @Override
    public CollegeInfo[] selectAll() {

        return collegeInfoMapper.selectAll();

    }


    //统计最受欢迎的前十个大学
    @Override
    public CollegeInfo[] selectTopTen() {

        CollegeInfo[] collegeInfos = this.selectAll();

        for (int i = 0; i < collegeInfos.length; i++) {
            for (int j = 0; j < collegeInfos.length - i - 1; j++) {
                if (collegeInfos[j].getTargetCollegeCount() < collegeInfos[j + 1].getTargetCollegeCount()) {
                    CollegeInfo temp = collegeInfos[j];
                    collegeInfos[j] = collegeInfos[j + 1];
                    collegeInfos[j + 1] = temp;
                }
            }
        }

        return collegeInfos;

    }
}
